﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;

public partial class TeacherEval_School_ScoreInput_TeacherOverView : ScoreInputBasePage
{
    protected int evalId = 0;
    private int evalType = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Int32.TryParse(Request["id"], out evalId))
            {
                evalId = 0; 
            }
            if (!Int32.TryParse(Request["evaltype"], out evalType))
                evalType = 0;

            //InitDisplay();
            InitGradeSelect();
            InitSubjectSelect();

            groupChanged(selGrade as object, new EventArgs());
        }
    }
    /// <summary>
    /// 初始化年级选择下拉框
    /// </summary>
    private void InitGradeSelect()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        IList<SchoolInfo> schoolList = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        selGrade.Items.Add(new ListItem("全部", "0"));
        switch (schoolList[0].SchoolOpt)
        {
            case 1:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                break;
            case 2:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 3:
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
            case 4:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 5:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
        }
    }

    /// <summary>
    /// 初始化科目选择下拉框
    /// </summary>
    private void InitSubjectSelect()
    {
        selSubject.Items.Add(new ListItem("全部", "0"));
        IList<GlobalCauseInfo> causeList = Container<IGlobalCauseInfoLogic>.Instance.SelectSchoolCause(SchoolMark, 1);
        if (causeList == null || causeList.Count < 1)
            return;

        foreach (GlobalCauseInfo info in causeList)
            selSubject.Items.Add(new ListItem(info.CauseName, info.RID.ToString()));
    }

    protected void groupChanged(object sender, EventArgs e)
    {
        int subjectId = -1;
        int gradeId = -1;
        if (!Int32.TryParse(selGrade.SelectedValue, out gradeId))
            return;
        if (!Int32.TryParse(selSubject.SelectedValue, out subjectId))
            return;

        List<TeacherRoleInfoView> list = null;
        if (subjectId > 0 && gradeId == 0)
            list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInSubject(subjectId, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        else if (subjectId == 0 && gradeId > 0)
            list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInGrade(gradeId, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        else if (subjectId > 0 && gradeId > 0)
        {
            list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInSubject(subjectId, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);
            List<TeacherRoleInfoView> templist1 = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindTeacherInGrade(gradeId, SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);

            if (list == null || list.Count < 1 || templist1 == null || templist1.Count < 1)
                return;
            List<TeacherRoleInfoView> templist = new List<TeacherRoleInfoView>();
            foreach (TeacherRoleInfoView view in list)
            {
                foreach (TeacherRoleInfoView infoView in templist1)
                {
                    if (view.GradeID == infoView.GradeID && infoView.RID == view.RID)
                    {
                        templist.Add(view);
                        break;
                    }
                }
            }
            list = templist;
        }
        else if (subjectId == 0 && gradeId == 0)
            list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindAllTeachers(SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, "Class_Teacher");

        if (list == null || list.Count < 1)
            return;

        List<TeacherRoleInfoView> tt = new List<TeacherRoleInfoView>();
        List<int> teacherIdList = new List<int>();
        foreach (TeacherRoleInfoView i in list)
        {
            if (!teacherIdList.Contains(i.RID))
            {
                teacherIdList.Add(i.RID);
                tt.Add(i);
            }
        }

        int managerId = Common.GetUserInfo().UID;
        if (evalId == 0)
        {
            string s = txtHistoryId.Text;
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out evalId);
        }
        IList<TeacherEvalState> stateList =
        Container<ITeacherEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                       SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                       SchoolMark, teacherIdList, evalId, managerId);

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;

        CreateTable(tt, stateList, termStart);
    }

    //private void InitDisplay()
    //{
    //    if (!Int32.TryParse(Request["id"], out evalId))
    //        evalId = 0;
    //    if (!Int32.TryParse(Request["evaltype"], out evalType))
    //        evalType = 0;

    //    List<TeacherRoleInfoView> list = null;
    //    list = (List<TeacherRoleInfoView>)Container<ITeacherRolesLogic>.Instance.FindAllTeachers(SchoolMark, SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm);

    //    if (list == null || list.Count < 1)
    //        return;

    //    List<TeacherRoleInfoView> tt = new List<TeacherRoleInfoView>();
    //    List<int> teacherIdList = new List<int>();
    //    foreach (TeacherRoleInfoView i in list)
    //    {
    //        if (!teacherIdList.Contains(i.RID))
    //        {
    //            teacherIdList.Add(i.RID);
    //            tt.Add(i);
    //        }
    //    }

    //    IList<TeacherEvalState> stateList =
    //    Container<ITeacherEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
    //                                                   SchoolCommon.Sch_SchoolInfo.SchoolTerm,
    //                                                   SchoolMark, teacherIdList, evalId);

    //    Hashtable ht = new Hashtable();
    //    ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
    //    ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
    //    IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
    //    if (infoList == null || infoList.Count < 1)
    //        return;
    //    DateTime termStart = (DateTime)infoList[0].StartDate;

    //    CreateTable(tt, stateList, termStart);
    //}
 

    /// <summary>
    /// 构造教师评价表格
    /// </summary>
    private void CreateTable(List<TeacherRoleInfoView> teacherList, IList<TeacherEvalState> stateList, DateTime termStart)
    {
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "教师";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<TeacherEvalState> sort = new ListSort<TeacherEvalState>(stateList, "StartTime", false);
        stateList = sort.Sort();

        int month = 0;
        int year = 0;
        string s = txtYearMonth.Text;
        if (!string.IsNullOrEmpty(s))
        {
            string[] arr = s.Split('.');
            int.TryParse(arr[0], out year);
            int.TryParse(arr[1], out month);
            
        }

        if (month > 0)
        {
            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}月", month);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            dateList.Add(termStart);
            termStart = termStart.AddMonths(1);
        }
        else
        {
            while (!(termStart.Month > DateTime.Now.Month && termStart > DateTime.Now))
            {

                hcell = new TableHeaderCell();
                hcell.Text = String.Format("{0}月", termStart.Month);
                hcell.Style[HtmlTextWriterStyle.Width] = "75px";
                width += 75;
                hrow.Cells.Add(hcell);
                dateList.Add(termStart);
                termStart = termStart.AddMonths(1);
            }
        }
        tbResult.Rows.Add(hrow);

        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (TeacherRoleInfoView info in teacherList)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = String.Format("{0}({1})", info.TeachName, info.TeachCode);
            row.Cells.Add(cell);

            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (TeacherEvalState state in stateList)
                {
                    if (state.TeacherID == info.RID && state.StartTime.Month == dt.Month)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        //control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
                        //                        year, term,
                        //                        state.ClassID, state.StartTime.Year, state.StartTime.Month);

                        control.NavigateUrl = String.Format("../ScoreInput/TeachInput.aspx?HistoryId={0}&TeacherId={1}&TeacherName={2}&TeacherCode={3}&Year={4}&Month={5}&Type=Edit",
                            evalId, info.RID, Server.UrlEncode(info.TeachName), info.TeachCode, state.StartTime.Year, state.StartTime.Month);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    //control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
                    //                        year, term,
                    //                        info.RID, dt.Year, dt.Month);
                    control.NavigateUrl = String.Format("../ScoreInput/TeachInput.aspx?HistoryId={0}&TeacherId={1}&TeacherName={2}&TeacherCode={3}&Year={4}&Month={5}&Type=Edit",
                             evalId, info.RID, Server.UrlEncode(info.TeachName), info.TeachCode, dt.Year, dt.Month);
                    cell.Controls.Add(control);
                }

                row.Cells.Add(cell);
            }

            tbResult.Rows.Add(row);
        }
    }

    protected void SetMonth()
    {
        Response.Write("if(document.getElementById('selMonth') != null)");
        Response.Write("{");
        Response.Write("    var selMonth = document.getElementById('selMonth');");

        Response.Write("    var option = document.createElement('option');");
        Response.Write("option.selected = '';");
        Response.Write("    option.innerText = '本学期';");
        Response.Write("    option.value = '0.0';");
        Response.Write("    selMonth.appendChild(option);");

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;

        while (!(termStart.Month > DateTime.Now.Month && termStart > DateTime.Now))
        {
            string str = "";
            switch (termStart.Month)
            {
                case 1: str = "一月"; break;
                case 2: str = "二月"; break;
                case 3: str = "三月"; break;
                case 4: str = "四月"; break;
                case 5: str = "五月"; break;
                case 6: str = "六月"; break;
                case 7: str = "七月"; break;
                case 8: str = "八月"; break;
                case 9: str = "九月"; break;
                case 10: str = "十月"; break;
                case 11: str = "十一月"; break;
                case 12: str = "十二月"; break;
                default: break;
            }

            Response.Write("var option = document.createElement('option');");
            Response.Write("option.innerText = '" + str + "';");
            Response.Write("option.value = '" + termStart.ToString("yyyy.MM") + "';");
            if (termStart.Month == DateTime.Now.Month)
            {
                Response.Write("option.selected = 'selected ';");
            }
            else
            {
                Response.Write("option.selected = '';");
            }
            Response.Write("selMonth.appendChild(option);");
            Response.Write("selMonth.click();");
            termStart = termStart.AddMonths(1);
        }
        Response.Write("}");


    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
      //  groupChanged(sender, e);
    }
}
